package graph.net;

import java.io.IOException;
import java.net.ServerSocket;

/**********************************************
 * @author amahmood
 * 
 *         Class that open the port and passes control to a worker thread
 * 
 *********************************************/
public class Ps1Server
{
    private static ServerSocket serverSocket = null;

    /**********************************************
     * @param port
     *********************************************/
    public static void init(int port)
    {
        try
        {
            serverSocket = new ServerSocket(port);

            System.out.println("Listening on port " + port);
        }
        catch (IOException e)
        {
            System.out.println("Could not listen on port " + port);
            System.exit(-1);
        }
        while (true)
        {
            ServerSession session;

            try
            {
                session = new ServerSession(serverSocket.accept());
                Thread t = new Thread(session);
                t.start();
            }
            catch (IOException e)
            {
                System.out.println("Accept failed: port");
                System.exit(-1);
            }
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        init(9000);
    }

}
